package rexsee.core.menu;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.lang.RexseeLanguage;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;

public class RexseeMenu implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Menu";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return childBrowser.menu;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeMenu(childBrowser);
	}

	public static final String ID_COMMON_MENU = "common_menu";
	public static final String DEFAULT_STYLE_OPTIONSMENU = "window-type:popup;window-outside-cancelable:true;window_align:center;window_vertical_align:bottom;height:200;window_dim_amount:0;border-width:2px;border-color:#222222;";

	private final Context mContext;
	private final Browser mBrowser;
	public final PopMenus menus;
	private String optionsMenuId = "";
	public boolean isOptionsMenuOpen = false;

	public RexseeMenu(Browser browser) {

		super();

		mBrowser = browser;
		mContext = browser.getContext();
		menus = new PopMenus();

		PopMenu commonMenu = new PopMenu(browser, ID_COMMON_MENU, "label:" + RexseeLanguage.TITLE_MENU + ";");
		commonMenu.add(new PopMenuItem(mContext, RexseeLanguage.ACTION_RELOAD, new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(mBrowser.application.resources.prefix + ":reload");
			}
		}, "label:" + RexseeLanguage.ACTION_RELOAD + ";"));
		commonMenu.add(new PopMenuItem(mContext, RexseeLanguage.ACTION_BACK, new Runnable() {
			@Override
			public void run() {
				mBrowser.function.goBack();
			}
		}, "label:" + RexseeLanguage.ACTION_BACK + ";"));
		commonMenu.add(new PopMenuItem(mContext, RexseeLanguage.ACTION_FORWARD, new Runnable() {
			@Override
			public void run() {
				mBrowser.function.goForward();
			}
		}, "label:" + RexseeLanguage.ACTION_FORWARD + ";"));
		commonMenu.add(new PopMenuItem(mContext, RexseeLanguage.ACTION_RESTART, new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(mBrowser.application.resources.prefix + ":restart");
			}
		}, "label:" + RexseeLanguage.ACTION_RESTART + ";"));
		commonMenu.add(new PopMenuItem(mContext, RexseeLanguage.ACTION_QUIT, new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(mBrowser.application.resources.prefix + ":quit");
			}
		}, "label:" + RexseeLanguage.ACTION_QUIT + ";"));
		commonMenu.add(new PopMenuItem(mContext, RexseeLanguage.TITLE_ABOUT, new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(mBrowser.application.resources.prefix + ":about");
			}
		}, "label:" + RexseeLanguage.TITLE_ABOUT + ";"));
		if (browser.application.resources.home.equals("")) {
			commonMenu.add(new PopMenuItem(mContext, RexseeLanguage.ACTION_SETUP, new Runnable() {
				@Override
				public void run() {
					mBrowser.function.load(mBrowser.application.resources.prefix + ":home_setup");
				}
			}, "label:" + RexseeLanguage.ACTION_SETUP + ";"));
		}
		menus.add(commonMenu);

		optionsMenuId = ID_COMMON_MENU;

	}

	public void composeOptionsMenu(Menu menu) {
		PopMenu popMenu = menus.get(optionsMenuId);
		if (popMenu != null) {
			popMenu.composeOptionsMenu(menu);
			isOptionsMenuOpen = true;
		} else {
			menu.clear();
			isOptionsMenuOpen = false;
		}
	}
	public void clickOptionsMenu(MenuItem item) {
		PopMenu popMenu = menus.get(optionsMenuId);
		if (popMenu != null) {
			Runnable runnable = popMenu.items.get(item.getItemId()).upRunnable;
			if (runnable != null) {
				runnable.run();
			} else {
				mBrowser.exception("OptionsMenu", "OptionsMenu item runnable is null.");
			}
		} else {
			mBrowser.exception("OptionsMenu", "OptionsMenu item is null.");
		}
	}

	public void open(PopMenu menu) {
		menu.open(mBrowser);
	}

	//JavaScript Interface
	public boolean exists(String menuId) {
		PopMenu popMenu = menus.get(menuId);
		return (popMenu == null) ? false : true;
	}
	public void create(String menuId) {
		create(menuId, "");
	}
	public void create(String menuId, String style) {
		PopMenu popMenu = menus.get(menuId);
		if (popMenu != null) {
			popMenu.items.clear();
			popMenu.styleSheet.parseStyle(style);
		} else {
			popMenu = new PopMenu(mBrowser, menuId, style);
			menus.add(popMenu);
		}
	}
	public void remove(String menuId) {
		menus.remove(menuId);
	}
	public void addItem(String menuId, String upUrl, String style) {
		addItem(menuId, "", upUrl, style, "", "");
	}
	public void addItem(String menuId, String upUrl, final String normalStyle, String pressedStyle, String selectedStyle) {
		addItem(menuId, "", upUrl, normalStyle, pressedStyle, selectedStyle);
	}
	public void addItem(String menuId, String downUrl, String upUrl, String normalStyle, String pressedStyle, String selectedStyle) {
		final String down_Url = mBrowser.urlListeners.getAbsoluteUrl(downUrl);
		final String up_Url = mBrowser.urlListeners.getAbsoluteUrl(upUrl);
		PopMenu popMenu = menus.get(menuId);
		if (popMenu != null) {
			Runnable downRunnable = (downUrl.equalsIgnoreCase("")) ? null : new Runnable() {
				@Override
				public void run() {
					mBrowser.function.load(down_Url);
				}
			};
			Runnable upRunnable = (upUrl.equalsIgnoreCase("")) ? null : new Runnable() {
				@Override
				public void run() {
					mBrowser.function.load(up_Url);
				}
			};
			PopMenuItem item = new PopMenuItem(mContext, up_Url, downRunnable, upRunnable, normalStyle, pressedStyle, selectedStyle);
			popMenu.add(item);
		}
	}
	public void open(String menuId) {
		PopMenu menu = menus.get(menuId);
		if (menu != null) {
			menu.open(mBrowser);
		}
	}

	public void setOptionsMenuId(String menuId) {
		optionsMenuId = menuId;
	}
	public void clearOptionsMenu() {
		optionsMenuId = "";
	}
	public String getOptionsMenuId() {
		return optionsMenuId;
	}

	public void openOptionsMenu() {
		((Activity) mContext).openOptionsMenu();
	}
	public void closeOptionsMenu() {
		((Activity) mContext).closeOptionsMenu();
	}

}
